package cn.lyd.org.controller;

import cn.lyd.basic.annotation.PreAuthorize;
import cn.lyd.basic.util.AjaxResult;
import cn.lyd.basic.util.PageList;
import cn.lyd.org.domain.Department;
import cn.lyd.org.query.DepartmentQuery;
import cn.lyd.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
//@Controller
//@ResponseBody

@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    /**
     * 接口：查询单个数据
     * @param id
     * @return
     */
    //前端传参 get http://localhost:8080/department/1

    //后端接收参数 @GetMapping("/department/{id}") + @PathVariable("id") Long id
        //@GetMapping等价于：@RequestMapping(     method = {RequestMethod.GET}   )
        //@PathVariable 从路径中获取变量
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "查询单个部门",sn="department:getOne")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    /**
     * 查询全部
     * @return
     */
    //前端传参  get http://localhost:8080/department {对象}

    //后端接收参数 @GetMapping("/department")
    @GetMapping
    @ApiOperation(value = "查询全部" )
    @PreAuthorize(name = "查询所有部门",sn="department:getAll")
    public List<Department> findAll(){
        return departmentService.findAll();
    }


    /**
     * 接口：分页查询
     * @param departmentQuery
     * @return
     */
    //前端传参 post http://localhost:8080/department 对象

    //后端接收参数 @PostMapping("/department")+ @RequestBody Department department
    //@PostMapping等价于：@RequestMapping(     method = {RequestMethod.POST}   )
    //@RequestBody从请求体【请求数据包中】中获取数据

    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "查询部门列表",sn="department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
      return   departmentService.queryPage(departmentQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    //delete http://localhost:8080/department/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    @PreAuthorize(name = "删除部门",sn="department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            departmentService.del(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    /**
     * 添加或修改
     * @param department
     * @return
     */
    //put http://localhost:8080/department {对象}
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    @PreAuthorize(name = "添加和修改部门",sn="department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            //判断有无id--判断是添加还是修改
            if (department.getId()==null){
                 departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            //操作成功调用AjaxResult的无参构造
            return new AjaxResult();
        } catch (Exception e) {//操作失败
            e.printStackTrace();
            //操作失败调用AjaxResult的有参构造
            return new AjaxResult(false,"操作失败");
        }

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除对象" )
    @PreAuthorize(name = "批量删除部门",sn="department:patchDel")
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            departmentService.patchDel(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树")
    @PreAuthorize(name = "查询部门树",sn="department:deptTree")
    public List<Department> deptTree(){
        return  departmentService.deptTree();
    }


}
